import React from "react";
import {withRouter} from "react-router-dom";
import {inject, observer} from "mobx-react";
import {WrAccordion, WrAlertPage, WrMenu, WrPicker, WrShow} from "../../../public";
import {
    ActionSheet,
    Bubble,
    Card,
    Icon,
    Toast,
    InputItem,
    List,
    Modal,
    Popover,
    TextareaItem,
    Tools
} from "weaver-mobile";
import {Upload, TabPage} from "weaver-mobile-page";
import * as util from '../../../util';
import Ellipsis from "./ellipsis";

const getLabel = Tools.getLabel;

/**
 * 评分-详情
 */
@inject("accessViewStore")
@withRouter
@observer
export default class Detail extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            tab: 0, //选中的tab值
            open: false //是否打开结果页详情
        }
        this.store = this.props.accessViewStore;
    }

    render() {
        let {titlename, canedit, cstatus, msg, remark, pesronstr, ccolor, tableHeader, datalist, itemcount, type1} = this.store;

        //获取datalist属于当前用户的考核指标
        const find = tableHeader.find(item => item.index == this.state.tab);
        //当前用户特有的考核指标
        let dataLength = datalist.length;
        const arr = datalist.filter((item, index) => index < itemcount && item.userid == find.userid)
        if (arr && arr.length == 0) {
            datalist = datalist.filter((item, index) => (index < itemcount && item.userid == "") || index >= itemcount)
        } else {
            datalist = datalist.filter((item, index) => (index < itemcount && item.userid == find.userid) || index >= itemcount);
        }

        let itemNum = itemcount - (dataLength - datalist.length);

        return [
            <div className="roll accessView-content-roll">
                <WrShow show={true}>
                    <div className="accessView-content-title">
                        <span className="accessView-content-title-name">{titlename}</span>
                        {msg == 1 && <span className="accessView-content-title-other">
                                <span className="accessView-content-title-audit"
                                      dangerouslySetInnerHTML={{__html: pesronstr}}></span>
                                <Popover
                                    mask
                                    visible={false}
                                    overlay={[
                                        (<Popover.Item key="0"><span
                                            className="workrelate-popover">{getLabel(24969, "相关链接")}</span></Popover.Item>),
                                        (<Popover.Item key="1"><span
                                            className="workrelate-popover">{getLabel(32061, "操作日志")}</span></Popover.Item>),
                                        (<Popover.Item key="2"><span
                                            className="workrelate-popover">{getLabel(33956, "意见反馈")}</span></Popover.Item>),
                                    ]}
                                    onSelect={this.handlePopoverSelect}
                                >
                                   <span className="accessView-content-title-more">
                                        <span className="workrelate-status" style={{background: ccolor}}>
                                            {cstatus}
                                        </span>
                                        <Icon type="right"/>
                                    </span>
                                 </Popover>
                             </span>}
                    </div>
                    {msg == 1 ? <div>
                        <TabPage
                            tabs={tableHeader}
                            style={{position: "sticky", top: 0, zIndex: 100}}
                            tabProps={{
                                onTabClick: (val) => {
                                    Toast.loading('Loading...', 0.5);
                                    this.setState({
                                        tab: val.index
                                    })
                                }
                            }}
                        />
                        <div className="accessView-content-detail">
                            <WrAccordion title={getLabel(513669, "考核单")}
                                         classStyle="accessView-content-accordion accessView-accordion-list">
                                {datalist && datalist.map((item, index) => {
                                    if (index < itemNum) {
                                        return <Card key={index} full>
                                            <Card.Header title={<List.FormItem
                                                label={getLabel(15486, "序号")}
                                                noBorder
                                                line
                                                content={<span dangerouslySetInnerHTML={{__html: (index + 1)}}/>}
                                            />}/>
                                            <Card.Body>
                                                {
                                                    item.cate && <List>
                                                        {this.buildFormItem("text", getLabel(455, "分类"), item.cate)}
                                                    </List>
                                                }
                                                <List>
                                                    {this.buildFormItem("text", getLabel(18085, "指标类型"), item.accessitemid)}
                                                </List>
                                                <List>
                                                    <Ellipsis label={getLabel(508669, "指标描述")}
                                                              value={item.description}/>
                                                </List>
                                                <List>
                                                    {this.buildFormItem("text", getLabel(6071, "权重"), item.rate)}
                                                </List>
                                                <List>
                                                    {this.buildItemtarget(item, index)}
                                                </List>
                                                <List>
                                                    {this.buildItemresult(item, index)}
                                                </List>
                                                <List>
                                                    {this.buildItemnext(item, index)}
                                                </List>
                                                <List>
                                                    {this.buildScore(item, index)}
                                                </List>
                                                <List>
                                                    {this.buildFormItem("text", getLabel(17506, "得分"), item["result_" + this.state.tab])}
                                                </List>
                                                <List>
                                                    {this.buildRemark(item, index)}
                                                </List>
                                            </Card.Body>
                                        </Card>
                                    }
                                })}
                            </WrAccordion>
                            <WrAccordion title={getLabel(513670, "评分信息")} classStyle="accessView-accordion-list">
                                {this.buildRevise(datalist, itemNum)}
                            </WrAccordion>
                            {remark && <WrAccordion title={getLabel(16965, "相关说明")}>
                                <div className="workrelate-accessview-remark" style={{width: window.innerWidth - 50}}
                                     dangerouslySetInnerHTML={{__html: remark}}>
                                </div>
                            </WrAccordion>}
                        </div>
                    </div> : <WrAlertPage show={msg != "" && msg != 1}
                                          msg={msg}
                                          icon={"wuquanxian"}/>}
                </WrShow>
            </div>,
            <WrShow show={msg == 1}>
                {this.buildResult()}
                {/*{this.buildOperateButton()}*/}
            </WrShow>,
            <WrMenu
                data={this.props.menu}
                onSelect={val => {
                    this.props.history.push(val.props.path)
                }}
                buttonData={this.getRightButton(itemNum)}
            />,

        ]
    }

    //处理
    handlePopoverSelect = (val, key) => {
        if (key == 0) {
            this.store.showtype = "relatelink";
        } else if (key == 1) {
            this.store.currentpage = 1;
            this.store.showtype = "log";
        } else if (key == 2) {
            this.store.showtype = "feedback";
        }
    }

    //构建最终得分
    buildResult = () => {
        const {open} = this.state;
        const {tableHeader} = this.store;
        if (open) {
            let result = this.getData(100);
            let totalDetail = this.getData(5);
            let result_array = [];
            tableHeader && tableHeader.map(item => {
                let result_map = item;
                result_map.resultscore = totalDetail["score_" + (item.index)];
                result_array[item.exeorder] = result_map;
            });
            return <div className="result-open" onClick={() => this.setState({open: !open})}>
                <div className="result-open-close"><Icon type="cross" size={"lg"}/></div>
                <div className="open-header">
                    <span>{getLabel(386205, "考核明细")}</span>
                    <span>{"*" + getLabel(513671, "考核实际情况请查看评分人单页")}</span>
                </div>
                <div className="open-detail">
                    {result_array && result_array.map((item, idx) => {
                        return <span className="open-item">
                            <span>
                                <span>{getLabel(508672, "评分人") + (idx + 1) + ":"}</span>
                                <span>{item.title}</span>
                            </span>
                            <span>
                                {item.resultscore}
                            </span>
                        </span>
                    })}
                </div>
                {result && <div className="open-result">
                    <span>{result.itemresult + ":"}</span>
                    <span>{result.score_0}</span>
                </div>}
            </div>
        } else {
            let result = this.getData(100);
            return result && <div className="result-close" onClick={() => this.setState({open: !open})}>
                <span>{result.itemresult + ":"}</span>
                <span>
                    <span>{result.score_0}</span>
                    <span>{getLabel(386205, "考核明细")}</span>
                </span>
            </div>

        }
    }

    getData = (onlyid) => {
        const {datalist} = this.store;
        return datalist.find(item => item.onlyid == onlyid);
    }

    //构建表单项
    buildFormItem = (type, label, value, isRequire, onChange) => {
        if (type == "text") {
            return <List.FormItem
                label={label + ":"}
                noBorder
                line
                className="text-span"
                content={<span dangerouslySetInnerHTML={{__html: value}}/>}
            />
        } else if (type == "textarea") {
            return <List.FormItem
                noBorder
                line
                required={value == "" && isRequire}
                extra={<Icon type="editor" size={"xxs"}/>}
                content={<TextareaItem
                    title={label + ":"}
                    viewattr="3"
                    escape={"true"}
                    placeholder={getLabel(83869, "请输入")}
                    ref={el => this.autoFocusInst = el}
                    autoHeight
                    value={value}
                    onChange={onChange}
                />}
            />
        } else if (type == "input") {
            return <List.FormItem
                label={label + ":"}
                noBorder
                line
                required={value == "" && isRequire}
                extra={<Icon type="editor" size={"xxs"}/>}
                content={<InputItem
                    value={value}
                    viewattr="3"
                    placeholder={getLabel(83869, "请输入")}
                    onChange={onChange}
                />}
            />
        } else if (type == "inputnumber") {
            return <List.FormItem
                label={label + ":"}
                noBorder
                line
                required={value === "" && isRequire}
                extra={<Icon type="editor" size={"xxs"}/>}
                content={<InputItem
                    value={value}
                    viewattr="3"
                    placeholder={getLabel(83869, "请输入")}
                    onChange={onChange}
                />}
            />
        }
    }

    //构建目标值
    buildItemtarget = (record, index) => {
        const {canedit} = this.store
        if (canedit && record["edit_" + this.state.tab] == 1 && !(record.itemtype == 2 && (record.formula == 2 || record.formula == 3))) {
            if (record.itemtype == 2) {  //formula!=0的时候才能触发change事件
                if (record.formula == 13) {
                    return <List.FormItem
                        label={getLabel(18087, "目标值") + ":"}
                        noBorder
                        line
                        required={record.itemtarget == "" && true}
                        extra={<Icon type="editor" size={"xxs"}/>}
                        content={<InputItem
                            value={record.itemtarget}
                            viewattr="3"
                            placeholder={getLabel(83869, "请输入")}
                            onChange={(v) => this.doChange(v, record.dataNum, "itemtarget")}
                            onBlur={(v) => this.doBlur(index)}
                        />}
                    />
                    // return this.buildFormItem("input",
                    //     getLabel(18087, "目标值"),
                    //     record.itemtarget,
                    //     true,
                    //     (v) => this.doChange(v, record.dataNum, "itemtarget", 3))
                } else {
                    return this.buildFormItem("inputnumber",
                        getLabel(18087, "目标值"),
                        record.itemtarget,
                        true,
                        (v) => this.doChange(v, record.dataNum, "itemtarget", 3))
                }
            } else {
                return this.buildFormItem("input",
                    getLabel(18087, "目标值"),
                    record.itemtarget,
                    false,
                    (v) => this.doChange(v, record.dataNum, "itemtarget"))
            }
        } else {
            return this.buildFormItem("text", getLabel("18087", "目标值"), record.itemtarget + record.unit)
        }
    }

    //构建完成值
    buildItemresult = (record, index) => {
        const {canedit} = this.store
        if (record.itemtype == 2 && (record.formula == 2 || record.formula == 3)) {
            return this.buildFormItem("text",
                getLabel(508618, "完成值"),
                getLabel(513673, "实际：{itemresult}有效：{validresult}").replace("{itemresult}", record.itemresult + record.unit).replace("{validresult}", record.validresult + record.unit))
        } else if (canedit && record["edit_" + this.state.tab] == 1) {
            if (record.itemtype == 2) {  //formula!=0的时候才能触发change事件
                if (record.formula == 13) {
                    return <List.FormItem
                        label={getLabel(508618, "完成值") + ":"}
                        noBorder
                        line
                        required={record.itemresult == "" && true}
                        extra={<Icon type="editor" size={"xxs"}/>}
                        content={<InputItem
                            value={record.itemresult}
                            viewattr="3"
                            placeholder={getLabel(83869, "请输入")}
                            onChange={(v) => this.doChange(v, record.dataNum, "itemresult")}
                            onBlur={(v) => this.doBlur(index)}
                        />}
                    />
                    // return this.buildFormItem("input",
                    //     getLabel(508618, "完成值"),
                    //     record.itemresult,
                    //     true,
                    //     (v) => this.doChange(v, record.dataNum, "itemresult", 3))
                } else {
                    return this.buildFormItem("inputnumber",
                        getLabel(508618, "完成值"),
                        record.itemresult,
                        true,
                        (v) => this.doChange(v, record.dataNum, "itemresult", 3))
                }
            } else {
                return this.buildFormItem("input",
                    getLabel(508618, "完成值"),
                    record.itemresult,
                    false,
                    (v) => this.doChange(v, record.dataNum, "itemresult"))
            }
        } else {
            return this.buildFormItem("text", getLabel(508618, "完成值"), record.itemresult + record.unit)
        }
    }

    //构建下次目标
    buildItemnext = (record, index) => {
        const {canedit, titlestr} = this.store
        if (canedit && record["edit_" + this.state.tab] == 1) {
            if (record.itemtype == 2) {
                return this.buildFormItem("inputnumber",
                    titlestr + getLabel(330, "目标"),
                    record.itemnext,
                    true,
                    (v) => this.doChange(v, record.dataNum, "itemnext"))
            } else {
                return this.buildFormItem("input",
                    titlestr + getLabel(330, "目标"),
                    record.itemnext,
                    false,
                    (v) => this.doChange(v, record.dataNum, "itemnext"))
            }
        } else {
            return this.buildFormItem("text", titlestr + getLabel(330, "目标"), record.itemnext + record.unit)
        }
    }

    //构建评分
    buildScore = (record, index) => {
        if (record["edit_" + this.state.tab] == 1 && (record.itemtype == 1 || (record.itemtype == 2 && (record.formula == 0 || record.formula == 4)))) {
            return this.buildFormItem("inputnumber",
                getLabel(25622, "评分"),
                record["score_" + this.state.tab],
                true,
                (v) => this.doScoreChange(v, record.dataNum, this.state.tab))
        }
        return this.buildFormItem("text", getLabel(25622, "评分"), record["score_" + this.state.tab])
    }

    //构建说明
    buildRemark = (record, index) => {
        if (record["edit_" + this.state.tab] == 1) {
            return this.buildFormItem("textarea",
                getLabel(25734, "说明"),
                record["remark_" + this.state.tab],
                false,
                (v) => this.doChange(v, record.dataNum, "remark_" + this.state.tab))
        }
        return this.buildFormItem("text", getLabel(25734, "说明"), record["remark_" + this.state.tab])
    }

    //构建考核说明
    buildRevise = (datalist, itemNum) => {
        const {scoreid, revisemax, revisemin, fileStore, category, maxUploadSize} = this.store;
        let arr = [];
        let {tab} = this.state;
        datalist && datalist.map((record, index) => {
            if (index >= itemNum) {
                if (record.onlyid == 2) {
                    let array = []
                    if (record["isedit" + tab] == 1 && !(revisemax == 0 && revisemin == 0)) {
                        array.push(this.buildFormItem("inputnumber",
                            record.itemresult,
                            record["score_" + tab],
                            false,
                            (v) => this.onResiveChange(v, record.dataNum, tab)))
                    } else {
                        array.push(this.buildFormItem("text",
                            record.itemresult,
                            record["score_" + tab]))
                    }
                    if (record["score_" + tab] != 0) {
                        if (record["isedit" + tab] == 1) {
                            array.push(this.buildFormItem("textarea",
                                getLabel(513397, "修正理由"),
                                record["reason" + tab],
                                true,
                                (v) => this.doChange(v, record.dataNum, "reason" + tab)))
                        } else {
                            array.push(this.buildFormItem("text", getLabel(513397, "修正理由"), record["reason" + tab],))
                        }
                    }
                    arr.push(<List>{array}</List>);
                } else if (record.onlyid == 3) {
                    let array = []
                    if (record["isedit" + tab] == 1) {
                        array.push(this.buildFormItem("textarea",
                            record.itemresult,
                            record["score_" + tab],
                            false,
                            (v) => this.doChange(v, record.dataNum, "score_" + tab)))
                    } else {
                        array.push(this.buildFormItem("text",
                            record.itemresult,
                            record["score_" + tab]))
                    }
                    arr.push(<List>{array}</List>)
                } else if (record.onlyid == 4) {
                    arr.push(<List><List.FormItem
                        label={record.itemresult + ":"}
                        noBorder
                        line
                        //required={!value && isRequire}
                        content={<div className="workrelate-accessView-upload"><Upload
                            viewAttr={record["isedit" + tab] == 1 ? 2 : 1}
                            store={fileStore}
                            uploadUrl={'/api/doc/upload/uploadFile2Doc?category=' + category}
                            datas={util.formatFiles(record["score_" + tab], record["isedit" + tab] == 1)}
                            editable={true}
                            onChange={(ids, files) => {
                                this.onFilesChange(ids, files, tab, record.dataNum, record["isedit" + tab] == 1)
                            }}
                            maxUploadSize={maxUploadSize}
                            showUploader={false}
                            showUploadTime={false}
                            onItemClick={(file, idx, files) => {
                                util.opendoc(file.fileid, scoreid, 3)
                            }}
                            onFileLoad={(file) => {
                                util.downloadsDoc(file.id, file.imagefileid, scoreid, 3)
                            }}
                        /></div>}
                    /></List>)
                } else if (record.onlyid != 100) {
                    arr.push(<List>{this.buildFormItem("text",
                        record.itemresult,
                        record["score_" + tab])}</List>)
                }
            }
        })
        return arr;
    }
    //附件改变
    onFilesChange = (ids, files, tab, dataNum, _edit) => {
        const {accessViewStore} = this.props;
        let index = accessViewStore.datalist.findIndex(item => item.dataNum == dataNum);
        if (ids && ids.length > 0) {
            accessViewStore.fileids = ids.toString();
            accessViewStore.datalist[index]["score_" + tab] = util.formatFiles(files, _edit);
        } else {
            accessViewStore.fileids = "";
            accessViewStore.datalist[index]["score_" + tab] = [];
        }
    }

    onResiveChange(v, dataNum, idx) {//修正分改变  index-datalist索引 idx-评分人索引
        const {accessViewStore} = this.props;
        const {canedit, datalist, revisemax, revisemin} = accessViewStore;
        let index = datalist.findIndex(item => item.dataNum == dataNum);
        if (isNaN(v) && v != "-") {
            v = "";
        }
        if (canedit && v != undefined && datalist[index]["score_" + idx] != v) {
            if (v != "-" && (v < revisemin || v > revisemax)) {
                if (v < revisemin) {
                    accessViewStore.datalist[index]["score_" + idx] = revisemin;
                }
                if (v > revisemax) {
                    accessViewStore.datalist[index]["score_" + idx] = revisemax;
                }
                accessViewStore.countscore(idx);
                Bubble.warning(getLabel(509182, "修正分值需控制在{revisemin}-{revisemax}之间").replace("{revisemin}", revisemin).replace("{revisemax}", revisemax), 3, {top: "40%"});
                return;
            }
            accessViewStore.canedit = false;
            accessViewStore.datalist[index]["score_" + idx] = v;
            accessViewStore.countscore(idx);
            accessViewStore.canedit = true;
        }
    }

    doScoreChange(v, dataNum, idx) {//定性指标直接评分 计算 index-datalist索引 idx-评分人索引
        const {accessViewStore} = this.props;
        const {canedit, datalist, scoremax, scoremin} = accessViewStore;
        if (isNaN(v) && v != "-") {
            v = "";
        }
        let index = datalist.findIndex(item => item.dataNum == dataNum);
        if (canedit && v != undefined && datalist[index]["score_" + idx] != v) {
            if (v != "-" && (v < scoremin || v > scoremax)) {
                Bubble.warning(getLabel(509183, "评分值需控制在{scoremin}-{scoremax}之间").replace("{scoremin}", scoremin).replace("{scoremax}", scoremax), 3, {top: "40%"});
                return;
            }
            accessViewStore.canedit = false;
            accessViewStore.datalist[index]["score_" + idx] = v;
            accessViewStore.updateScore(index, idx);
            accessViewStore.canedit = true;
        }
    }

    // v当前改变的值  index当前索引 type-改变的值type  resetcount-是否重新计算评分 1-是
    doChange(v, dataNum, type, resetcount) {
        const {accessViewStore} = this.props;
        const {canedit, datalist} = accessViewStore;
        if (resetcount && resetcount > 0) {
            if (isNaN(v) && v != "-") {
                v = "";
            }
        }
        let index = datalist.findIndex(item => item.dataNum == dataNum);
        if (canedit && v != undefined && datalist[index][type] != v) {
            if (resetcount != 3) {
                accessViewStore.canedit = false;
            }
            accessViewStore.datalist[index][type] = v;
            if (resetcount == 1 || resetcount == 3) {
                accessViewStore.updateNormScore(index);
            } else {
                accessViewStore.canedit = true;
            }
        } else if (canedit && resetcount == 3) {
            accessViewStore.canedit = false;
            accessViewStore.datalist[index][type] = v;
            accessViewStore.canedit = true;
        }
    }

    doBlur(index) {//java类指标使用
        const {accessViewStore} = this.props;
        accessViewStore.updateNormScore(index);
    }

    //获取有权限的操作按钮
    getRightButton(itemNum) {
        const {msg, canback, canreset, canedit, caninit, canaudit, canrecover, canbackpre, checksize} = this.store;
        let menu = [];
        if (msg != 1) {
            return menu;
        }
        if (canedit) {
            menu.push({
                content: getLabel(30986, "保存"),
                onClick: key => this.doOperation("save")
            });
            menu.push({
                content: getLabel(615, "提交"),
                onClick: key => this.doOperation("submit", itemNum)
            });
        }
        if (canaudit) {
            menu.push({
                content: getLabel(142, "批准"),
                onClick: key => this.doApproveAndReturn("approve", getLabel("508666", "确定批准此考核结果"), getLabel("142", "批准"))
            });
            menu.push({
                content: getLabel(236, "退回"),
                onClick: key => this.doApproveAndReturn("return", getLabel("508667", "确定退回此考核结果"), getLabel("236", "退回"))
            });
            if (checksize > 1) {
                menu.push({
                    content: getLabel(508880, "退回上一级"),
                    onClick: key => this.doOtherOperation("return_pre", getLabel("508876", "确定退回此考核结果到上一个考核人?"), getLabel("508877", "退回此考核结果到上一个考核人"), 2)
                });
            }
        }
        if (canback) {//退回
            menu.push({
                content: getLabel(236, "退回"),
                onClick: key => this.doOtherOperation("score_return", getLabel(508667, "确定退回此考核结果"), getLabel(508881, "退回此考核结果"))
            });
        }
        if (canbackpre) {//退回上一级
            menu.push({
                content: getLabel(508880, "退回上一级"),
                onClick: key => this.doOtherOperation("return_pre", getLabel(508876, "确定退回此考核结果到上一个考核人?"), getLabel(508877, "退回此考核结果到上一个考核人"), 1)
            });
        }
        if (canreset) {//重新考核
            menu.push({
                content: getLabel(508884, "重新考核"),
                onClick: key => this.doOtherOperation("reset", getLabel(508886, "确定重新进行考核评分？"), getLabel(508884, "重新考核"))
            });
        }
        if (caninit) {//重置
            menu.push({
                content: getLabel(2022, "重置"),
                onClick: key => this.doOtherOperation("init", getLabel(508887, "确定重置考核项及结果？"), getLabel(2022, "重置"))
            });
        }
        if (canrecover) {
            menu.push({
                content: getLabel(18666, "收回"),
                onClick: key => this.doOtherOperation("recover", getLabel(508889, "确定收回考核项及结果？"), getLabel(18666, "收回"))
            });
        }
        return menu;
    }

    //获取操作菜单
    buildOperateButton = () => {
        let menu = this.getRightButton();
        if (menu.length > 0) {
            return <div className="workrelate-button">
                {menu.length < 4 && menu.map((item, index) => {
                    return <span key={index} onClick={item.onClick}>{item.content}</span>
                })}
                {menu.length > 3 && ([
                    <span key={0} onClick={() => menu[0].onClick()}>{menu[0].content}</span>,
                    <span key={1} onClick={() => menu[1].onClick()}>{menu[1].content}</span>,
                    <span key={2} onClick={() => this.showActionSheet(menu)}>{getLabel(17499, "更多")}</span>
                ])}
            </div>
        }
    }

    //展示更多操作按钮
    showActionSheet = (buttonData) => {
        let data = [...buttonData];
        data.push({content: getLabel(31129, "取消")})
        ActionSheet.showActionSheetWithOptions({
            options: data,
            cancelButtonIndex: data.length - 1,
            maskClosable: true,
        }, buttonIndex => data[buttonIndex].onClick);
    }

    //保存或提交操作
    doOperation(type, itemNum) {
        const {accessViewStore} = this.props;
        if (type) {
            accessViewStore.currenttype = type;
        }
        if (type == "submit" && !this.checkMust(itemNum)) {
            Bubble.warning(getLabel(509185, "请完整填写考核分数;定量指标需填写目标值完成值及{titlestr}目标;有修正分时需填写修正理由").replace("{titlestr}", accessViewStore.titlestr), 5);
            return false;
        }
        if (type == "submit") {//提交时候需要判断必填值是否必填
            Modal.alert(getLabel(386786, "信息确认"), getLabel(508894, "确定提交考核结果？"), [
                {text: getLabel(31129, "取消")},
                {text: getLabel(826, "确定"), onPress: () => this.doSaveAndSubmit()}
            ])
        } else {
            this.doSaveAndSubmit();
        }
    }

    //校验参数
    checkMust(itemNum) {
        const {accessViewStore: {datalist, tableHeader, itemcount}} = this.props;
        let flag = true;
        let cidx = "";

        tableHeader.map((element, idx) => {
            let arr = datalist.filter((item, index) => index < itemcount && item.userid == element.userid);
            if (arr && arr.length == 0) {
                arr = datalist.filter((item, index) => (index < itemcount && item.userid == "") || index >= itemcount)
            }
            arr.map((record, index) => {
                if (record["edit_" + element.index] == 1) {
                    cidx = element.index;
                    if ((record.itemtype == 1 || (record.itemtype == 2 && (record.formula == 0 || record.formula == 4)))) {//定性指标
                        if (record["score_" + element.index] === "") { //定性类指标没有评分
                            flag = false;
                        }
                    } else {//定量指标没有填写完成值 目标值和下个周期目标值
                        if (record.itemtarget === "" || record.itemresult === "" || record.itemnext === "") {
                            flag = false;
                        }
                    }
                }
            })
        })

        datalist.map((record, index) => {
            if (index >= itemNum && record.onlyid == 2) {
                if (record["score_" + cidx] != 0 && record["reason" + cidx] === "") {
                    flag = false;
                }
            }
        });
        return flag;
    }

    //执行其他操作
    doOtherOperation(type, content, info, isaudit) {
        const {doOtherOperation} = this.store;
        Modal.alert(getLabel(386786, "信息确认"), content, [
            {text: getLabel(31129, "取消")},
            {
                text: getLabel(826, "确定"),
                onPress: () => doOtherOperation({operation: type, isaudit: isaudit}, this.props.updateListData)
            }
        ])
    }

    //处理批准或退回操作
    doApproveAndReturn(type, content, info) {
        const {doApproveAndReturn} = this.store;
        Modal.alert(getLabel(386786, "信息确认"), content, [
            {text: getLabel(31129, "取消")},
            {text: getLabel(826, "确定"), onPress: () => doApproveAndReturn({operation: type}, this.props.updateListData)}
        ])
    }

    //执行保存和提交
    doSaveAndSubmit() {
        const {accessViewStore} = this.props;
        if (accessViewStore.uploading) {
            this.refs.accessTables.wrappedInstance.saveUpload();
        } else {
            this.doLastSubmit();
        }
    }

    //最后的提交
    doLastSubmit() {
        const {saveAccess, currenttype} = this.store;
        saveAccess({operation: currenttype}, this.props.updateListData);
    }

    //获取表头
    buildHeader = () => {
        const {tabs, nowyear, stabList, type, type1, type2, resourceid, getTableDatas, onSearchChange} = this.store;
        return (
            <div className="accessView-header">
                <Popover
                    placement="bottomLeft"
                    onSelect={this.onSelect}
                    visible={false}
                    overlay={
                        <div className="workrelate-header-item">
                            {tabs.map(item => <Popover.Item key={item.key}>{item.title}</Popover.Item>)}
                        </div>
                    }
                >
                    <span className="accessView-type">
                        <span>{this.getType()}</span>
                        <Icon size="xs" type="down"/>
                    </span>
                </Popover>
                <WrPicker
                    type={type1}
                    minyear={2013}
                    maxyear={new Date().getFullYear() + 1}
                    initval={[nowyear, type2]}
                    onChange={val => this.store.accessView({resourceid, type1, year: val[0], type2: val[1]})}
                />
                <div style={{flex: 1}}/>
            </div>
        )
    }

    getType = () => {
        const {tabs, type1} = this.store;
        var find = tabs.find(item => item.key == type1);
        return find && find.title;
    }

    onSelect = (val) => {//顶部tab切换
        const {resourceid, nowyear, type1, nowmonth, nowseason} = this.store;
        let type2 = "";
        if (val.key == 1) {//月
            type2 = nowmonth;
        } else if (val.key == 2) {
            type2 = nowseason;
        }
        this.store.accessView({scoreid: "", resourceid, year: nowyear, type1: val.key, type2});
    }
}